﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using Cms.BLL;
using System.Text;
using Cms.EntLib;

namespace Cms.Web.UIBuilder
{
    public partial class UIDesign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Id = EntLib.Request.GetString("id");
            string param = EntLib.Request.GetString("param");
            if (!String.IsNullOrEmpty(Id))
            {
                if (param == "model")
                {
                    BLL.Sys_Selffom bll = new Sys_Selffom();
                    Model.Sys_Selfform model = bll.GetById(Id, false);
                    if (model != null)
                    {
                        FormSetting = Json.ToJson(model, model.GetType());
                        DBSetting = GetDBSetting(model);
                    }
                }
                else if(param=="list"){
                    BLL.Sys_Selffom_List_Layout bll = new Sys_Selffom_List_Layout();
                   Model.Sys_Selfform_List_Layout model= bll.GetById(Id, false);
                   if (model != null) { 
                   // FormSetting =
                       FormSetting = "{Tablename:'list'}";
                       DBSetting ="{list:"+ GetDBSettingBySQL(model.Sql)+"}";
                   }
                }

                FormSource = String.Format("http://{0}/UIBuilder/DesignForm.ashx?formid={1}&param={2}&action=getformjson", EntLib.Request.GetCurrentFullHost(), Id, param);                
            }
            else
            {
                //FormAction = "add";
            }
        }

        public string GetDBSetting(Model.Sys_Selfform model) {
            StringBuilder sb = new StringBuilder();
            //主表的JSON
            sb.Append("{");
            sb.AppendFormat("{0}:{1},",model.Tablename,GetDBSetting(model.Tablename));
            foreach (Model.Sys_Selfform_Detail md in model.Detail) {
                sb.AppendFormat("{0}:{1},", md.Tablename, GetDBSetting(md.Tablename));
            }
            if (sb.Length >0 &&sb[sb.Length-1] == ',')
                sb.Remove(sb.Length - 1, 1);
            sb.Append("}");
           
            return sb.ToString();
        }
        public string GetDBSetting(string tablename) {
            BLL.BaseDao<Model.Sys_Datadict_Field, string> bllfield = new BaseDao<Cms.Model.Sys_Datadict_Field, string>();
            IList fieldlist = bllfield.Find("select t.Fieldname,t.Caption from Sys_Datadict_Field t where t.Table.Tablename='" + tablename + "'", null);
            return Newtonsoft.Json.JsonConvert.SerializeObject(fieldlist);

        }
        public string GetDBSettingBySQL(string SQL)
        {
            BLL.BaseDao<Model.Sys_Datadict_Field, string> bllfield = new BaseDao<Cms.Model.Sys_Datadict_Field, string>();
            //IList fieldlist = bllfield.Find("select t.Fieldname,t.Caption from Sys_Datadict_Field t where t.Table.Tablename='" + tablename + "'", null);
            //return Newtonsoft.Json.JavaScriptConvert.SerializeObject(fieldlist);
            IList fieldlist = bllfield.GetColumns(SQL);
            return Newtonsoft.Json.JsonConvert.SerializeObject(fieldlist);


        }


        public string StrConvert(string str)
        {
            string result = "";
            string[] ss = str.Split('_');
            foreach (string s in ss)
            {
                if (!String.IsNullOrEmpty(s) && s.Length > 0)
                    result += s.Substring(0, 1).ToUpper() + s.Substring(1);
                else
                {
                    result += s;
                }
            }
            return result.TrimEnd('_');
        }


        public string DBSetting { get; set; }
        public string FormSetting { get; set; }

        //public string FormAction { get; set; }
        public string FormSource { get; set; }
    }

}
